<?php
namespace Admin\Controller;
use Think\Controller;
class BackstageController extends CommonController {
    public function lst(){
        $cate=D('cate');
        $where=1;
        if($kw=I('kw')){
            $where.=' AND name LIKE "%'.$kw.'%" or keywords LIKE "%'.$kw.'%"';
            $list = $cate->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        }
        else{
            $list = $cate->where($where)->limit($Page->firstRow.','.$Page->listRows)->catetree();
        }
//        $count= $cate->where($where)->count();// 查询满足要求的总记录数
//        $Page= new \Think\Page($count,6);// 实例化分页类 传入总记录数和每页显示的记录数(25)catetree
//        $show= $Page->show();// 分页显示输出
//        $list = $cate->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('cate',$list);// 赋值数据集
        $this->display();



    }
    public function add(){
        if(IS_POST) {
            $cate = D('cate');
            $data['parentid'] = I('parentid');
            $data['name'] = I('name');
            $data['type'] = I('type');
            $data['class'] = I('class');
            $data['keywords'] = I('keywords');
            $data['des'] = I('des');
            $data['content'] = I('content');
            if ($_FILES['pic']['tmp_name'] != '') {
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 3145728;// 设置附件上传大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->savePath = './Public/Uploads/'; // 设置附件上传目录
                $upload->rootPath = './';//网站的根路径
                // 上传文件
                $info = $upload->uploadOne($_FILES['pic']);//单个文件上传
                if (!$info) { // 上传错误提示错误信息
                    $this->error($upload->getError());
                } else { // 上传成功
                    $data['pic'] = $info['savepath'] . $info['savename'];
                }

            }
            if ($cate->create($data)) {
                if ($cate->add()) {
                    $this->success('新增栏目成功！', U('lst'));
                } else {
                    $this->error('新增栏目失败！');
                }

            } else {
                $this->error($cate->getError());
            }
            return;
        }
        $cate=D('cate');
        $cates=$cate->catetree();
        $this->assign('cates',$cates);
        $this->display();
    }
    public function edit(){
          $cate = D('cate');
        if(IS_POST) {
            $data['parentid'] = I('parentid');
            $data['name'] = I('name');
            $data['id'] = I('id');
            $data['type'] = I('type');
            $data['class'] = I('class');
            $data['keywords'] = I('keywords');
            $data['des'] = I('des');
            $data['content'] = I('content');
            if ($_FILES['pic']['tmp_name'] != '') {
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 3145728;// 设置附件上传大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->savePath = './Public/Uploads/'; // 设置附件上传目录
                $upload->rootPath = './';//网站的根路径
                // 上传文件
                $info = $upload->uploadOne($_FILES['pic']);//单个文件上传
                if (!$info) { // 上传错误提示错误信息
                    $this->error($upload->getError());
                } else { // 上传成功
                    $data['pic'] = $info['savepath'] . $info['savename'];
                }

            }
            if ($cate->create($data)) {
                if ($cate->save()) {
                    $this->success('修改栏目成功！', U('lst'));
                } else {
                    $this->error('修改栏目失败！');
                }

            } else {
                $this->error($cate->getError());
            }
            return;
        }
        $cateid=I('id');
        $cateres=$cate->find($cateid);
        $this->assign('cateres',$cateres);
        $cates=$cate->catetree();
        $this->assign('cates',$cates);
        $this->display();
    }
    public function del(){
        $cate=D('cate');
        $id=I('id');
        if($cate->delete($id)){
            $this->success('删除栏目成功！', U('lst'));
        }else{
            $this->error('删除栏目失败！');
        }
    }

    public function bdel()
    {
        $cate=D('cate');
        $ids=I('ids');
//        var_dump($ids);die;
        $ids=implode(',', $ids);  //1,2,3,4
        if($ids)
        {
            if($cate->delete($ids))
            {
                $this->success('批量删除栏目成功！',U('lst'));
            }else
            {
                $this->error('批量删除栏目失败！');
            }
        }else
        {
            $this->error('未选中任何内容！');
        }
    }

    public function sortcate()
    {
        $cate=D('cate');
        foreach ($_POST as $id=>$sort) {
            $cate->where("id=$id")->setField('sort',$sort);//setField()修改字段的值
        }

        $this->success('成功更新栏目顺序！',U('lst'));
    }
    public function logout(){
        session(null);
        $this->success('退出成功！',U('Login/index'));
    }
    public function adminedit(){
        $admin=D('admin');
        if(IS_POST){
            if($admin->create()){
                $admin->password=md5($admin->password);
                if($admin->save()){
                    $this->success('修改管理员成功！',U('lst'));
                }else{
                    $this->error('修改管理员失败');
                }
            }else{
                $this->error($admin->getError());
            }

            return;
        }
        $id=I('id');
        $adminres=$admin->find($id);
        $this->assign('adminres',$adminres);
        $this->display();
    }



}
